import 'package:badges/badges.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:panda/base/application.dart';
import 'package:panda/event/NumEvent.dart';
import 'package:panda/event/NumEvent2.dart';
import 'package:panda/event/UserInfoEvent.dart';
import '../base/Constant.dart';

class MyBottomBar2 extends StatefulWidget {
  final int index;
  final ValueChanged<int> onChanged;

  const MyBottomBar2({Key key, this.index = 0, this.onChanged})
      : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new MyBottomBar2State();
  }
}

class MyBottomBar2State extends State<MyBottomBar2>
    with AutomaticKeepAliveClientMixin<MyBottomBar2> {
  int _tabIndex = 2;
  var tabImages = [
    [
      Image.asset('images/tab_home_nomal.png',
          width: 24, height: 24, alignment: Alignment.bottomCenter),
      Image.asset('images/tab_home_selected.png',
          width: 24, height: 24, alignment: Alignment.bottomCenter)
    ], 
    [
      Image.asset('images/tab_cart_nomal.png',
          width: 24, height: 24, alignment: Alignment.bottomCenter),
      Image.asset('images/tab_cart_selected.png',
          width: 24, height: 24, alignment: Alignment.bottomCenter)
    ],
    [
      Image.asset('images/tab_mine_nomal.png',
          width: 24, height: 24, alignment: Alignment.bottomCenter),
      Image.asset('images/tab_mine_selected.png',
          width: 24, height: 24, alignment: Alignment.bottomCenter)
    ]
  ];

  var appBarTitles = ['首页', "购物车", '我的'];
  int nums = 0;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    APPLIC.eventBus.on<NumEvent>().listen((event) {
      nums = event.num;
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return new BottomNavigationBar(
      backgroundColor: white,
      items: <BottomNavigationBarItem>[
        new BottomNavigationBarItem(icon: getTabIcon(0), title: getTabTitle(0)),
        new BottomNavigationBarItem(icon: getTabIcon(1), title: getTabTitle(1)),
        new BottomNavigationBarItem(
            icon: Stack(
              children: <Widget>[
                getTabIcon(2),
                Positioned(
                  child: Offstage(
                    child: Badge(
                      padding: EdgeInsets.all(4),
                      badgeColor: mainColor,
                      shape: BadgeShape.circle,
                      toAnimate: false,
                      badgeContent: Text('$nums',
                          style: TextStyle(
                              color: Colors.white,
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(SMALL_FONT_SIZE))),
                    ),
                    offstage: nums == 0,
                  ),
                  right: 0,
                  top: 0,
                )
              ],
            ),
            title: getTabTitle(2)),
        // new BottomNavigationBarItem(icon: getTabIcon(3), title: getTabTitle(3)),
      ],
      type: BottomNavigationBarType.fixed,
      //默认选中首页
      currentIndex: _tabIndex,
      iconSize: 24.0,
      //点击事件
      onTap: _onTapHandler,
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  /*
   * 根据选择获得对应的normal或是press的icon
   */
  Image getTabIcon(int curIndex) {
    if (curIndex == _tabIndex) {
      return tabImages[curIndex][1];
    }
    return tabImages[curIndex][0];
  }

  /*
   * 获取bottomTab的颜色和文字
   */
  Text getTabTitle(int curIndex) {
    if (curIndex == _tabIndex) {
      return new Text(appBarTitles[curIndex],
          style: new TextStyle(
              fontSize: ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
              color: mainColor));
    } else {
      return new Text(appBarTitles[curIndex],
          style: new TextStyle(
              fontSize: ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
              color: const Color(0xff515151)));
    }
  }

  void _onTapHandler(int index) {
    APPLIC.eventBus.fire(NumEvent2(index));
    Navigator.of(context).pop();
    if(APPLIC.HH==1){
          Navigator.of(context).pop();
                    Navigator.of(context).pop();

APPLIC.HH=0;
    }
  }
}
